package com.gd.coreservice.controller;


import com.gd.commonutils.Constants;
import com.gd.commonutils.Result;
import com.gd.coreservice.entity.Collection;
import com.gd.coreservice.entity.vo.CollectionVo;
import com.gd.coreservice.service.CollectionService;
import com.gd.servicebase.exceptionhandler.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-03-23
 */
@RestController
@RequestMapping("/collection")
@CrossOrigin
public class CollectionController {
    @Autowired
    CollectionService collectionService;


    //根据学生id查询数据
    //根据输入的用户id 去collection表中找到所有的此id记录  然后根据每行记录中的商品id去商品表中查出此id购买的商品的所有信息 最后根据商品名称去过滤出用户购买该商品的信息
   //已测试
    @GetMapping("/getCollection")
    public Map<String, Object> findCollection(@RequestParam("uid") String uid, @RequestParam("itemName") String itemName){
        List<CollectionVo> data = collectionService.findCollection(uid,itemName);
//        Integer total = collectionService.getUidTotal(uid,itemName);     这样就只需去后台查询一次 提高效率
        Integer total = data.size();
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    //根据用户id和商品id查询数据是否存在  即该用户是否收藏了该商品
    //已测试
    @GetMapping("/getMyCollection")
    public Boolean findMyCollection(@RequestParam("uid") String uid,@RequestParam("gid") String gid){
        return collectionService.findMyCollection(uid,gid);
    }

    //增加
    //已测试
    @PostMapping("/insertCollection")
    public Result addCollection(@RequestParam("uid") String uid, @RequestParam("gid") String gid) {
        if(collectionService.insertCollection(uid,gid))
        {
            return Result.success("收藏成功");
        }
        else
        {
            throw new ServiceException(Constants.CODE_600, "后台异常");//自定义异常
        }

    }


    @DeleteMapping("/deleteCollection/{uid}&{itemId}")
    public Integer deleteCollection(@PathVariable("uid") String uid,@PathVariable("itemId") String itemId){
        return collectionService.deleteCollection(uid,itemId);
    }
}

